<?php

namespace app\admin\controller\annalgame;

use app\common\controller\Backend;
use think\Log;
use think\Db;
use think\Controller;
use think\Request;

/**
 * 
 *游戏帐变
 * @icon fa fa-circle-o
 */
class Annalchange extends Backend
{
    /**
     * 跨库关联查询关联键
     */
    protected $kuamap = [];
    
    /**
     * 跨库关联查询关联键
    */
    protected $kuaid = 'uid';

    /**
     * 跨库关联读取关建键表
     */
    protected $kuatablea = 'fa_v_annal_game';

    /**
     * 跨库关联提取字段表
     */
    protected $kuatableb = 'user';

    /**
     * 跨库关联提取字段字符串
     */
    protected $fieldstr= 'uid,nickname,sex,level';

    /**
     * VAnnalGame模型对象
     */
    protected $model = null;
    public function _initialize()
    {   
        parent::_initialize();
        $this->model = model('VAnnalGame');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
    
        //进行获取信息
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
    
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
    
            //搜索条件
            $filter = $this->request->get("filter", '');
            $filter_arr = json_decode($filter,true);
            $map = [];
            foreach ($filter_arr as $key=>$val){
                if($key == 'createtime'){
                    $time_arr = explode(',', $filter_arr['createtime']);
                    $star = $this->getMilliDate($time_arr[0]);
                    $end = $this->getMilliDate($time_arr[1]);
                    if(empty($time_arr[0])){
                        $map['createtime'] = array('<=', $end);
                    }elseif(empty($time_arr[1])){
                        $map['createtime'] = array('>=', $star);
                    }else{
                        $map['createtime'] = array('between', [$star,$end]);
                    }
                }else{
                    $map[$key] = $val;
                }
            }
            
            //房卡消耗数量
            $gametimenum = Db::table('fa_v_annal_game')->field('sum(gametime) as gametimenum')->where($map)->find();
            if(empty($gametimenum['gametimenum'])){
                $gametimenum['gametimenum'] = 0;
            }
            
            //获取总数条数
            $total = Db::table('fa_v_annal_game')
            ->field('uid,kinId,createtime,COUNT(roomid) as win,roomid,sum(changes) as changes,sum(gametime) as gametime')
            ->where($map)
            ->group('uid,createtime,roomid,kinId')
            ->count();
    
            //10条数据
            $tableInfo = Db::table('fa_v_annal_game')
            ->field('uid,kinId,createtime,ctype,COUNT(roomid) as win,roomid,sum(changes) as changes,sum(gametime) as gametime')
            ->where($map)
            ->group('uid,createtime,roomid,kinId')
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
        
            if(empty($tableInfo))
            {
                $result = array("total" => 0, "rows" => [],"gametimenum"=>0,"map"=>empty($map)?0:1);
                return json($result);
            }
            $this->kuamap = array_column($tableInfo, 'uid');
            
            //获取一条结构，进行用于重组数据
            $list = $this->model
            ->where($map)
            ->order($sort, $order)
            ->limit('0', '1')
            ->select();
            
            $list = $this->changedata($list[0],$tableInfo);
    
            //重组生成数据,进行时间重装;
            foreach($list as $key=>$value){
                $list[$key]['gametime'] = $this->Sec2Time($value['gametime']);
            }
            $result = array("total" => $total, "rows" => $this->SetNamepost($this->kuakfunc(),$list),"gametimenum"=>$this->Sec2Time($gametimenum['gametimenum']),"map"=>empty($map)?0:1);
            
            return json($result);
        }
    
        return $this->view->fetch();
    }

}
